package org.openehr.am.validation;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import java.util.ResourceBundle;
/**
 * Archetype validation error with error type, short text 
 * and a longer more specific explanation of the error
 * 
 * @author rong.chen
 * @author sebastian.garde
 */
public class ValidationError {
	
	public static ResourceBundle errorDescriptions;
	
	public ValidationError(ErrorType type) {
		this(type, null);
	}
	
	public ValidationError(ErrorType type, String text) {
		this.type = type;
		this.text = text;
	}
	
	public ErrorType getType() {
		return type;
	}
	
	public String getText() {
		return text;
	}
	
	public String getDescription() {
		if (errorDescriptions == null)
			errorDescriptions = ResourceBundle.getBundle("validations");
		if (description == null) 
			description = errorDescriptions.getString(this.type.toString());
		return description;
	}	
	
	

	
	/**
	 * String representation of this error
	 */
	public String toString() {
		return type + ", " + text + ", "+ getDescription();
	}
	
	public boolean equals(Object obj) {
		   if (obj == null) { return false; }
		   if (obj == this) { return true; }
		   if (obj.getClass() != getClass()) {
		     return false;
		   }
		   ValidationError ve = (ValidationError) obj;
		   return new EqualsBuilder()
		                 .append(type, ve.type)
		                 .append(text, ve.text)
		                 .append(description, ve.description)
		                 .isEquals();
	}
	
	public int hashCode() {
	     return new HashCodeBuilder(11, 47).
	       append(type).
	       append(text).
	       append(description).
	       toHashCode();
	   }

	

	// fields
	private ErrorType type;
	private String text;
	private String description;	
}
